package flca.voicenav.view;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import flca.voicenav.model.GpxRoute;
import flca.voicenav.model.TrkPt;

public class RouteOverlay extends Overlay
{
	private GpxRoute gpxRoute=null;
	private MapView mapView;
	
    public RouteOverlay(GpxRoute gpxRoute) {
		super();
		this.gpxRoute = gpxRoute;
	}

	@Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    {
        super.draw(canvas, mapView, shadow);                   
        this.mapView = mapView;
        
        //-- Create new paint object --
        Paint mPaint = drawRouteOpts();

        for (int i=1; i < gpxRoute.getCoordinates().size(); i++) {
        	Point p1 = getP(gpxRoute.getCoordinates().get(i-1));
        	Point p2 = getP(gpxRoute.getCoordinates().get(i));
            canvas.drawLine(p1.x , p1.y, p2.x, p2.y, mPaint);			
		}

        return true;
    }

	private Paint drawRouteOpts()
	{
		Paint mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);
		return mPaint;
	}
    
    private Point getP(TrkPt aTrkPt)
    {
    	Point result = new Point();
    	
        GeoPoint geopnt = new GeoPoint((int) (aTrkPt.getLat() * 1E6), (int) (aTrkPt.getLng() * 1E6));
        mapView.getProjection().toPixels(geopnt,  result);
        return result;
    }
} 

